package com.example.myapplication

import android.view.View
import android.widget.ScrollView
import com.google.android.material.floatingactionbutton.FloatingActionButton
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class test10_ScrollView : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_test10_scroll_view)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        // 获取 ScrollView 和 FloatingActionButton 的引用
        val scrollView = findViewById<ScrollView>(R.id.scrollView)
        val fabScrollTop = findViewById<FloatingActionButton>(R.id.fabScrollTop)

// 设置滚动监听器
        scrollView.viewTreeObserver.addOnScrollChangedListener {
            val scrollY = scrollView.scrollY
            // 滚动超过200像素时显示按钮
            if (scrollY > 200) {
                fabScrollTop.show()
            } else {
                fabScrollTop.hide()
            }
        }

// 设置悬浮按钮点击事件
        fabScrollTop.setOnClickListener {
            scrollView.smoothScrollTo(0, 0)
        }

    }
}